
package com.b2mining.integration.xml;

import com.b2mining.integration.beans.XMLBean;
//import com.b2mining.integration.helper.Helper;

import java.io.*;

import org.apache.log4j.Logger;
import org.jdom.Document;
import org.jdom.input.SAXBuilder;


/**
 * Define la clase XMLMapper
 * @since (7/3/2003 10:35:26 AM)
 * @author Synopsis S.A. <BR>
 * Analista Programador: Miguel Lora G.,Janyn Roca
 * MODIFIED by AAV 20040430						
 */
public abstract class XMLMapper
{
	Logger log = Logger.getLogger(this.getClass());
   /** @param bean 
     * @param stream */
   public abstract void generateXML(XMLBean bean, Writer stream) throws XMLMapperException;
   
   /** @param stream */
   public abstract XMLBean parseXML(Reader stream, Reader streamAux)throws XMLMapperException;
   
   public abstract void generateXML(XMLBean beanList, Writer stream, int contador) throws XMLMapperException;

	protected Document validaDocumento(Reader reader, Reader readerAux, XMLMapperException ex) {
		log.debug("Valida documento XML");
			SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser", true);
			builder.setValidation(true);
			Document doc =null;
	
		boolean isIncorrect= false;
			try {
				log.debug("antes de construir un documento");
				log.debug(reader.toString());
				doc = builder.build( reader );
				log.debug("Doc "+doc.toString());
			} catch (Exception e) {
				log.debug("Error Validadando documento XML 1" + e.getMessage() + "Error "+ e.toString());
				//System.out.println("!!!ESTRUCTURA INVALIDA");
				e.printStackTrace();
				isIncorrect = true; 
				XMLMapperException mape = new StructureException(e.getMessage() );
				ex.addChild(mape);			
			}
			
			if (isIncorrect) {
				builder.setValidation(false);
				log.debug("Valido falso");
				try {
					doc = builder.build( readerAux );
				} catch (Exception e) {
					log.debug("Error Validadando documento XML 2");
				//	System.out.println("!!!ERROR:"+e);
					XMLMapperException mape = new StructureException(e.getMessage() );
					ex. addChild(mape);			
				}
			}

			if (doc!=null) {
				log.debug("No soy nulo en el documento");
				validaContenido(doc,ex);
			}
			
			return doc;
		
	}
	
	protected Document validaDocumento(String s, XMLMapperException ex) {
			SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser", true);

			builder.setValidation(true);
			Document doc =null;
			StringReader sr = new StringReader(s);
			boolean isIncorrect= false;
			try {
				log.debug("------------antes de 'doc = builder.build( sr );'");
				doc = builder.build( sr );
				log.debug("------------despues de 'doc = builder.build( sr );'");
			} catch (Exception e) {
				log.debug("------------error del 'doc = builder.build( sr );'");
				isIncorrect = true; 
				XMLMapperException mape = new StructureException(e.getMessage() );
				ex. addChild(mape);			
			}
			
			if (isIncorrect) {
				builder.setValidation(false);
			}
			log.debug("111111 :::::::::::::::::::::::::::::: ");
			if (doc!=null) {
				validaContenido(doc,ex);
			}
			log.debug("222222 :::::::::::::::::::::::::::::: ");
			sr.close();
			log.debug("333333 :::::::::::::::::::::::::::::: ");
			return doc;
	}

	protected Document validaDocumento2(String s, XMLMapperException ex) {
		SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser", true);

		builder.setValidation(true);
		Document doc =null;
		StringReader sr = new StringReader(s);
		boolean isIncorrect= false;
		try {
			log.debug("------------antes de 'doc = builder.build( sr );'");
			doc = builder.build( sr );
			log.debug("------------despues de 'doc = builder.build( sr );'");
		} catch (Exception e) {
			log.debug("------------error del 'doc = builder.build( sr );'");
			isIncorrect = true; 
			XMLMapperException mape = new StructureException(e.getMessage() );
			ex. addChild(mape);			
		}
		
		if (isIncorrect) {
			builder.setValidation(false);
		}
		log.debug("111111 :::::::::::::::::::::::::::::: ");
		if (doc!=null) {
			try {
				validaContenido2(doc,ex);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				log.debug("ERROR EN VALIDACONTENIDO2");
			}
		}
		log.debug("222222 :::::::::::::::::::::::::::::: ");
		sr.close();
		log.debug("333333 :::::::::::::::::::::::::::::: ");
		return doc;
}
	
	protected abstract void validaContenido(Document doc , XMLMapperException ex) ;
	protected abstract void validaContenido2(Document doc , XMLMapperException ex) ;

}